﻿using System;
using DMS.BaseClass;
using DMS.Business.Utility;
using DMS.Business.Modules.Organization;

namespace DMS.Web.Modules.BaseSettings
{
    public partial class ExtendUserView : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string mode = Page.Request["mode"];
                string categoryId = Request["categoryId"];
                string deptName = Request["deptName"];
                
                
                if (mode.Equals("new"))
                {//新增目录
                    ViewState["title"] = "添加第三方人员信息";
                    if (categoryId.Equals("null"))
                    {
                        ParentClassID.Value = "";
                    }
                    else
                    {
                        this.tb_DeptName.Text = deptName;
                    }
                }
                else if (mode.Equals("edit"))
                {//编辑
                    ViewState["title"] = "修改第三方人员信息";
                    FillFormInfo();
                } else
                if (mode.Equals("view"))
                {//查看
                    ViewState["title"] = "查看第三方人员信息";

                    FillFormInfo();
                    this.tb_UserName.ReadOnly = true;
                    this.tb_Mobile.ReadOnly = true;
                    this.tb_positionDesc.ReadOnly = true;
                    this.ddl_sex.Enabled = false;
                    this.btnSave.Visible = false;
                }
            }
        }

        public void FillFormInfo() {
            string userID = Request["userid"];

            ExtendUser exUser = ExtendUserHelper.Instance.GetExtendUserByID(userID);

            this.tb_positionDesc.Text = exUser.PositionName;
            this.tb_UserName.Text = exUser.UserName;
            this.ddl_sex.SelectedValue = ((int)exUser.Sex).ToString();
            this.tb_Mobile.Text = exUser.Mobile;
            this.tb_DeptName.Text = exUser.Dept.Name;
            this.hd_deptID.Value = exUser.DeptID.ToString();
            //this.tb_DeptName.ReadOnly = false;
        }

        public void SaveExtendUser(object sender, EventArgs e)
        {
            try
            {
                string mode = Page.Request["mode"];
                string categoryId = Request["categoryId"];
                string userID = Request["userid"];

                ExtendUser exUser = null;
                string id = Guid.NewGuid().ToString();
                Department dept = null;

                if (mode.Equals("new"))
                {
                    dept = UserHelper.GetDepartmentByID(categoryId);
                }
                else if (mode.Equals("edit"))
                {
                    id = userID;
                    dept = UserHelper.GetDepartmentByID(this.hd_deptID.Value);
                }

                exUser = new ExtendUser(id);
                exUser.Dept = dept;
                exUser.PositionName = this.tb_positionDesc.Text;
                exUser.UserName = this.tb_UserName.Text;
                exUser.Sex = (SexTypes)Enum.Parse(typeof(SexTypes), this.ddl_sex.SelectedValue);
                exUser.Mobile = this.tb_Mobile.Text;

                if (mode.Equals("new"))
                {
                    if (!ExtendUserHelper.Instance.SaveExtendUser(exUser))
                    {
                        ClientHelper.Instance.RenderScript(this.Page, "", "alert('该用户已经存在，请勿重复添加。');");
                    }
                    else
                    {
                        ClientHelper.Instance.CloseWindow("保存成功！", "1", RefreshType.NoRefresh);
                    }
                }
                else if (mode.Equals("edit"))
                {

                    if (!ExtendUserHelper.Instance.UpdateExtendUser(exUser))
                    {
                        ClientHelper.Instance.RenderScript(this.Page, "", "alert('用户名及手机号码不唯一，请勿重复添加。');");
                    }
                    else
                    {
                        ClientHelper.Instance.CloseWindow("保存成功！", "1", RefreshType.NoRefresh);
                    }
                }
          
            }
            catch (Exception ei)
            {
                LogHelper.LogException(ei);
                ClientHelper.Instance.RenderScript(this.Page, "", "alert('保存失败，请联系管理员');window.close();");
            }
        }
    }
}